unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, Menus,
  StdCtrls, IniPropStorage, BGRAButton, BGRALabel, BGRAPanel, BGRAWin7ToolBar,
  BaseUnix, Unix, process;

type

  { TFormMain }

  TFormMain = class(TForm)
    BGRAButton1: TBGRAButton;
    BGRAButtonRus: TBGRAButton;
    BGRAButtonEng: TBGRAButton;
    BGRAButtonFra: TBGRAButton;
    BGRAButtonUkr: TBGRAButton;
    BGRAPanel1: TBGRAPanel;
    IniPropStorage1: TIniPropStorage;
    procedure BGRAButton1Click(Sender: TObject);
    procedure BGRAButtonEngClick(Sender: TObject);
    procedure BGRAButtonFraClick(Sender: TObject);
    procedure BGRAButtonRusClick(Sender: TObject);
    procedure BGRAButtonUkrClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  FormMain: TFormMain;
  language,prog_dir:string;

implementation

{$R *.lfm}


function ForkStartProgram (current_dir,path:string;const S:Array Of AnsiString):longint;
var
  child_pid:longint;
begin
  FpChdir(current_dir);
  child_pid:=FpFork;
  ForkStartProgram:=child_pid; // Возвращаем результат fork
  if child_pid<0 then
  begin
    exit; //error
  end;
  if child_pid=0 then
  begin
    fpsignal (SIGHUP,signalhandler(SIG_IGN)); // Отцепляем подчиненный процесс
    fpexeclp (path,S);
  end;
end;

{ TFormMain }
procedure SelectButton(Button_:TBGRAButton;Pressed:boolean);
begin
  if Pressed then
  begin
    Button_.BodyNormal.BorderColor:=clLime;
    Button_.BodyNormal.Gradient1.StartColor:=clWhite;
    Button_.BodyNormal.Gradient1.EndColor:=clLime;
    Button_.BodyNormal.Gradient2.StartColor:=clLime;
    Button_.BodyNormal.Gradient2.EndColor:=clWhite;
  end
  else
  begin
    Button_.BodyNormal.BorderColor:=$00AB713B;
    Button_.BodyNormal.Gradient1.StartColor:=$00EFE6D2;
    Button_.BodyNormal.Gradient1.EndColor:=$00C87511;
    Button_.BodyNormal.Gradient2.StartColor:=$00C87511;
    Button_.BodyNormal.Gradient2.EndColor:=$00EFE6D2;
  end  ;
end;

procedure InitLang();
var
  lang:string;
begin
  lang:=FormMain.IniPropStorage1.ReadString('language','en');
  SelectButton(FormMain.BGRAButtonRus,false);
  if lang='ru' then
  begin
    SelectButton(FormMain.BGRAButtonRus,true)
  end;
  SelectButton(FormMain.BGRAButtonEng,false);
  if lang='en' then
  begin
    SelectButton(FormMain.BGRAButtonEng,true)
  end;
  SelectButton(FormMain.BGRAButtonUkr,false);
  if lang='ua' then
  begin
    SelectButton(FormMain.BGRAButtonUkr,true)
  end;
  language:=lang;
  SelectButton(FormMain.BGRAButtonFra,false);
  if lang='fr' then
  begin
    SelectButton(FormMain.BGRAButtonFra,true)
  end;
  language:=lang;
end;

procedure TFormMain.BGRAButton1Click(Sender: TObject);
begin
  FormMain.WindowState:=wsMinimized;
  ForkStartProgram (GetCurrentDirUTF8+'/PhET/sims/semiconductor/','java',['-Djavaws.user.language='+language,'-jar','semiconductor_all.jar']);
  //Edit1.Text:=language;
  {
  Process1.Active:=false;
  Process1.CurrentDirectory:=GetCurrentDirUTF8+'/PhET/sims/semiconductor/';
  Process1.Executable:='java';
  Process1.Parameters.Clear;
  Process1.Parameters.Add('-Djavaws.user.language='+language);
  Process1.Parameters.Add('-jar');
  Process1.Parameters.Add('semiconductor_all.jar');
  Process1.Execute; }
end;

procedure TFormMain.BGRAButtonEngClick(Sender: TObject);
begin
  IniPropStorage1.WriteString('language','en');
  IniPropStorage1.Save;
  InitLang();
end;

procedure TFormMain.BGRAButtonFraClick(Sender: TObject);
begin
  IniPropStorage1.WriteString('language','fr');
  IniPropStorage1.Save;
  InitLang();
end;

procedure TFormMain.BGRAButtonRusClick(Sender: TObject);
begin
  IniPropStorage1.WriteString('language','ru');
  IniPropStorage1.Save;
  InitLang();
end;

procedure TFormMain.BGRAButtonUkrClick(Sender: TObject);
begin
  IniPropStorage1.WriteString('language','ua');
  IniPropStorage1.Save;
  InitLang();
end;

procedure TFormMain.FormCreate(Sender: TObject);
begin
  language:='ru';
  prog_dir:=GetUserDir+DirectorySeparator+'.hpet';
  if not DirectoryExistsUTF8(prog_dir) then ForceDirectoriesUTF8(prog_dir);
  IniPropStorage1.IniSection:='Options';
  IniPropStorage1.IniFileName:=prog_dir+DirectorySeparator+'prog_properties.ini';
  InitLang();
end;

end.

